package state.old;

public class GumballMachine {
    final static int SOLD_OUT = 0;
    final static int NO_QUARTER = 1;
    final static int HAS_QUATER = 2;
    final static int SOLD = 3;

    int state = SOLD_OUT; //糖果机状态，初始为售罄
    int count = 0;//

    public GumballMachine(int count) {
        this.count = count;
        if (count > 0) {
            state = NO_QUARTER;
        }
    }

    public void insertQuarter() {
        if (state == HAS_QUATER) {
            System.out.println("you cant insert another quarter");
        } else if (state == NO_QUARTER) {
            System.out.println("you inserted a quarter");
        } else if (state == SOLD_OUT) {
            System.out.println("you cant insert a quarter the machine is sold out");
        } else if (state == SOLD) {
            System.out.println("please wairt we already giving you a gumball");
        }
    }

    public void ejectQuarter() {
        if (state == HAS_QUATER) {
            System.out.println("Quarter returned");
            state = NO_QUARTER;
        } else if (state == NO_QUARTER) {
            System.out.println("You havent insert a quarter");
        } else if (state == SOLD) {
            System.out.println("you cant eject you hanvent insert a quarter yet");
        }
    }

    public void tunCrank() {
        if (state == SOLD) {
            System.out.println("Turning twice desnt get you anohter gumbabll");
        } else if (state == NO_QUARTER) {
            System.out.println("You turned but there is no gumbles");
        } else if (state == HAS_QUATER) {
            System.out.println("you turned");
            state = SOLD;
            dispense();
        }
    }

    public void dispense() {
        if (state == SOLD) {
            System.out.println("A gumball comes rolling out the slot");
            count = count - 1;
            if (count == 0) {
                System.out.println("oops, out of gumballs");
                state = SOLD_OUT;
            } else {
                state = NO_QUARTER;
            }
        } else if (state == NO_QUARTER) {
            System.out.println("you need pay first");
        } else if (state == SOLD_OUT) {
            System.out.println("no no  no no");
        }

    }

    @Override
    public String toString() {
        return "GumballMachine{" +
                "state=" + state +
                ", count=" + count +
                '}';
    }
}
